package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;


    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        //根据id有没有 判断是否更新还是添加
        boolean isUpdate = roomSubmitVo.getId() != null && roomSubmitVo.getId() != 0;
        //更新或新增房间信息
        this.saveOrUpdate(roomSubmitVo);
        // 获取请求传过来的ID
        Long roomId = roomSubmitVo.getId();
        if (isUpdate) {
            //删除房间相关的图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, roomId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除房间相关的属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //删除房间相关的配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //删除房间相关的标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //删除房间相关的支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //删除房间相关的可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        // 添加房间所对应的图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
        }
        // 添加房间相关的属性
        List<Long> AttrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValues = new ArrayList<>();
        if (!CollectionUtils.isEmpty(AttrValueIds)) {
            for (Long attrValueId : AttrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomId).attrValueId(attrValueId).build();
                roomAttrValues.add(roomAttrValue);
            }
        }
        // 添加房间相关的配套
        List<Long> FacilityIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(FacilityIds)) {
            for (Long facilityId : FacilityIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomId).facilityId(facilityId).build();
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //添加房间相关的标签
        List<Long> LabelIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabels = new ArrayList<>();
        if (!CollectionUtils.isEmpty(LabelIds)) {
            for (Long labelId : LabelIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomId).labelId(labelId).build();
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //添加房间相关的支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //添加房间相关的可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page, queryVo);
    }

    @Override
    public RoomDetailVo findRoomDetailVoById(Long roomId) {
        //根据房间id查询房间信息
        RoomInfo roomInfo = this.getById(roomId);
        //查询房间所对应的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        //查询房间所对应的图片列表
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoList(ItemType.ROOM, roomId);
        //查询房间所对应的属性列表
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.findAttrValueVoList(roomId);
        //查询房间所对应的配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.findFacilityInfoList(roomId);
        //查询房间所对应的标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.findLabelInfoList(roomId);
        //查询房间所对应的支付方式列表
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.findPaymentTypeList(roomId);
        //查询房间所对应的可选租期列表
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.findLeaseTermList(roomId);

        RoomDetailVo roomDetailVo = new RoomDetailVo();

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        return roomDetailVo;
    }

    @Override
    public void removeRoomInfoById(Long roomId) {
        this.removeById(roomId);

        //删除房间相关的图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, roomId);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //删除房间相关的属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //删除房间相关的配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //删除房间相关的标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //删除房间相关的支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //删除房间相关的可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




